/*
 * Copyright [2009] [Marcin Rzeźnicki]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
package mr.go.yaxc.actions;

import java.util.ArrayList;

import mr.go.yaxc.Action;
import mr.go.yaxc.ScopeHandler;

import org.xml.sax.Attributes;

/**
 * This action creates a list from its <i>params</i> which it passes to parent's
 * <i>params</i> (if present), or, if <i>id</i> attribute is set, puts it in the
 * global context
 * 
 * <h6>on xml element start:</h6> creates a new, empty list, which is set as
 * <i>object</i> and passed as <i>params</i>. If <i>id</i> attribute is set list
 * is put in global context. If parent's <i>params</i> are present this list is
 * added to them <h6>on xml element end:</h6> nothing <h6>
 * prerequisites:</h6> none <h6>
 * attributes:</h6>
 * <table>
 * <tr>
 * <th>Attribute name</th>
 * <th>What is it for?</th>
 * <th>Required?</th>
 * </tr>
 * <tr>
 * <td><i>id</i></td>
 * <td>if set, puts created list in global context</td>
 * <td>no</td>
 * </tr>
 * </table>
 *<p>
 * Example:<br>
 * 
 * <pre>
 * 		&lt;List&gt;
 * 				&lt;String&gt;a&lt;/String&gt;
 * 				&lt;String&gt;b&lt;/String&gt;
 * 				&lt;String&gt;c&lt;/String&gt;
 * 		&lt;/List&gt;
 * </pre>
 * 
 * @author Marcin Rzeźnicki
 * 
 */
public class CreateList extends Action {

    @Override
    public void carryOut(String name, Attributes attributes,
                         ScopeHandler handler)
            throws Exception {
        String id = attributes.getValue(getAttribute("id"));
        final ArrayList<Object> arrayList = new ArrayList<Object>();
        object = arrayList;
        params = arrayList;
        if (id != null) {
            handler.setReference(id,
                                 object);
            if (handler.getLogger().isDebugEnabled()) {
                handler.getLogger().debug("Id '{}': {}",
                                          id,
                                          object.toString());
            }
        }
        handler.tryAddParam(object);
    }

    @Override
    public void processed(String body, ScopeHandler handler)
            throws Exception {
    }
}
